﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.MainClasses;
using GMS.ServiceFunctions.Data;

namespace GMS.Groups
{
    public partial class AddEditGroups : ChildForms
    {
        BindingSource BS;
        public enum EditMode
        { 
            Добавление,
            Редактирование,
            Удаление        
        }
        private EditMode formMode;
        public EditMode FormMode
        {
            get { return formMode; }
            set { formMode = value; }
        }
        private Int32 idGroup;
        public Int32 IdGroup
        {
            get { return idGroup; }
            set { idGroup = value; }
        }
        public AddEditGroups(EditMode FormMode)
        {
            InitializeComponent();

            this.FormMode = FormMode;

            StoredProcedures = new String[4];
            StoredProcedures[0] = "[SelectGroups]";
            StoredProcedures[1] = "[AddGroup]";
            StoredProcedures[2] = "[EditGroup]";
            StoredProcedures[3] = "[DeleteGroup]";

            if (FormMode == EditMode.Добавление)
            {
                Text = "Добавление группы";            
            }
            if (FormMode == EditMode.Редактирование)
            {
                Text = "Редактирование группы";

            }
            if (FormMode == EditMode.Удаление)
            {
                Text = "Удаление группы";

                textBox1.Enabled = false;
                textBox2.Enabled = false;
             }
        }

        private void acceptButton1_Click(object sender, EventArgs e)
        {
            if (FormMode == EditMode.Добавление)
            {
                MRF.ExecuteSqlCommand(StoredProcedures[1] + " '" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "'", ConnectionString);
            }
            if (FormMode == EditMode.Редактирование)
            {
                MRF.ExecuteSqlCommand(StoredProcedures[2] + " " + IdGroup + ",'" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "'", ConnectionString);
            }
            if (FormMode == EditMode.Удаление)
            {
                MRF.ExecuteSqlCommand(StoredProcedures[3] + " " + IdGroup, ConnectionString);
            }
        }

        private void AddEditGroups_Load(object sender, EventArgs e)
        {

            BS = new BindingSource();
            BS.DataSource = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0]);
            BS.DataMember = "table";

            BS.Filter = "IdGroup = " + IdGroup;

            if (FormMode == EditMode.Редактирование || FormMode == EditMode.Удаление)
            {
                textBox1.DataBindings.Add("Text", BS, "Название группы");
                textBox2.DataBindings.Add("Text", BS, "Описание группы");
            }
        }
    }
}
